/**
 * 
 */
package com.nari.heatshare.action;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.json.JSONUtil;
import org.apache.struts2.json.annotations.JSON;

import com.nari.baseinfo.model.IBuilding;
import com.nari.baseinfo.model.ICommunity;
import com.nari.baseinfo.model.IConcentrator;
import com.nari.baseinfo.model.IHeatRoom;
import com.nari.baseinfo.model.IProject;
import com.nari.baseinfo.service.IBuildingService;
import com.nari.baseinfo.service.ICommunityService;
import com.nari.baseinfo.service.IConcentratorService;
import com.nari.baseinfo.service.IHeatRoomService;
import com.nari.baseinfo.service.IProjectService;
import com.nari.coreframe.action.struts2.JSONActionSupport;
import com.nari.heatshare.model.IAssignMeter;
import com.nari.heatshare.service.IAssignMeterService;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;
import com.nari.sink.bo.PSinkInfo;
import com.nari.sink.service.ISinkBiz;
import com.opensymphony.xwork2.Action;

/**
 * 
 * @author Xiaopeng
 * @date 2013-8-18
 */
public class AssignMeterAction extends JSONActionSupport {
	private static final long serialVersionUID = -8251250848823782839L;
	private IAssignMeterService iAssignMeterService;
	private IProjectService iProjectService;
	private ICommunityService iCommunityService;
	private IBuildingService iBuildingService;
	private IHeatRoomService iHeatRoomService;
	private ISinkBiz iSinkBiz;
	private IConcentratorService iConcentratorService;
	//分配表信息查询
	private String seriousNum;
	private String projectUID;
	private String communityUID;
	private String buildingUID;
	private String heatRoomUID;
	private List<IProject> projectList;
	private List<ICommunity> communityList;
	private List<IBuilding> buildingList;
	private List<IHeatRoom> heatRoomList;
	//分页查询
	private int pageIndex = 1; //默认查询第一页列表
	private PageCond pageCond;
	private int pageSize;
	private ResultWithPageCond<IAssignMeter> assignMeterPage;
	private Map<String, String> pageConcentratorMap;
	private Map<String, String> pageSinkerMap;
	//分配表信息管理
	private String assignMeterUID;
	private IAssignMeter assignMeter;
	private List<PSinkInfo> heatSinkerList;
	private List<IConcentrator> concentratorList;
	//查询分配表详细资料
	private IProject project;
	private ICommunity community;
	private IBuilding building;
	private IHeatRoom heatRoom;
	private IConcentrator concentrator;
	private PSinkInfo heatSinker;
	//通过集中器信息查询分配表列表
	private String concentratorUID;
	private List<IAssignMeter> assignMeterList;
	private Map<String, String> referBuildingMap;
	private Map<String, String> referRoomMap;
	
	/**
	 * 分页查询分配表列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String queryAssignMeterPage() throws Exception {
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//所属热用户
		this.heatRoomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectUID, communityUID, null, buildingUID);
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.assignMeterPage = this.iAssignMeterService.queryAssignMeterPage(seriousNum, projectUID, communityUID, buildingUID, heatRoomUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.assignMeterPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.assignMeterPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//集中器id-concentratorNo映射表
		this.pageConcentratorMap = this.iAssignMeterService.queryReferConcentratorMap(assignMeterPage.getList());
		//散热器id-sinkNo映射表
		this.pageSinkerMap = this.iAssignMeterService.queryReferHeatSinkerMap(assignMeterPage.getList());
		return "meterPage";
	}
	
	/**
	 * 分配表信息管理
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String queryMeterForManage() throws Exception {
		if(assignMeterUID!=null && !"".equals(assignMeterUID)) {
			this.assignMeter = this.iAssignMeterService.queryEntityById(assignMeterUID);
			//所属项目
			this.projectUID = this.assignMeter.getProjectID();
			//所属小区
			this.communityUID = this.assignMeter.getCommunityID();
			//所属楼栋
			this.buildingUID = this.assignMeter.getBuildingID();
			//所属热用户
			this.heatRoomUID = this.assignMeter.getRoomId();
			//当前分配表已挂接的散热器
			this.heatSinker = this.iSinkBiz.queryEntityById(assignMeter.getHeatSinkID());
		}
		//所属项目列表
		this.projectList = this.iProjectService.queryAllProjectInOrder();
		if(this.projectList == null) {
			this.projectList = new ArrayList<IProject>();
		}
		//所属小区列表
		if(this.projectUID!=null && !"".equals(projectUID)) {
			this.communityList = this.iCommunityService.queryCommunitysByBelongings(null, null, null, projectUID);
		} else {
			this.communityList = new ArrayList<ICommunity>();
		}
		//所属楼栋列表
		this.buildingList = this.iBuildingService.queryBuildingByBelongings(projectUID, communityUID);
		//所属热用户列表
		this.heatRoomList = this.iHeatRoomService.queryHeatRoomsByBelongs(projectUID, communityUID, null, buildingUID);
		//所属集中器列表
		this.concentratorList = this.iConcentratorService.queryConcentratorsByBelongs(projectUID, communityUID, null);
		//可绑定散热器列表
		this.heatSinkerList = this.iSinkBiz.queryBindableSinkerByBelongs(projectUID, communityUID, buildingUID, heatRoomUID);
		//分配表信息修改时，加载此分配表已挂接散热器
		if(this.heatSinker != null) {
			this.heatSinkerList.add(this.heatSinker);
		}
		return "meterManage";
	}
	
	/**
	 * 分配表新增或修改
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String saveOrUpdateMeterInfo() throws Exception {
		if(this.assignMeter.getId()!=null && !"".equals(assignMeter.getId())) {
			this.iAssignMeterService.updateEntity(assignMeter, assignMeter.getId());
		} else {
			this.iAssignMeterService.saveEntity(assignMeter);
		}
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * 根据唯一标识删除分配表信息
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String deleteAssignMeterByUID() throws Exception {
		this.iAssignMeterService.deleteEntityById(assignMeterUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(ResponseMsg.getInstance(true, "")));
		return Action.SUCCESS;
	}
	
	/**
	 * 查询分配表详细资料
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-8-29
	 */
	public String queryMeterDetail() throws Exception {
		if(assignMeterUID!=null && !"".equals(assignMeterUID)) {
			this.assignMeter = this.iAssignMeterService.queryEntityById(assignMeterUID);
			//所属项目
			this.project = this.iProjectService.queryEntityById(assignMeter.getProjectID());
			//所属小区
			this.community = this.iCommunityService.queryEntityById(assignMeter.getCommunityID());
			//所属楼栋
			this.building = this.iBuildingService.queryEntityById(assignMeter.getBuildingID());
			//所属热用户
			this.heatRoom = this.iHeatRoomService.queryEntityById(assignMeter.getRoomId());
			//集中器
			this.concentrator = this.iConcentratorService.queryEntityById(assignMeter.getConcentratorID());
			//挂接散热器
			this.heatSinker = this.iSinkBiz.queryEntityById(assignMeter.getHeatSinkID());
		}
		return "meterDetail";
	}
	
	/**
	 * 通过所属关系查询分配表列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-2
	 */
	public String queryAssignMetersByBelongs() throws Exception {
		List<IAssignMeter> meterList = this.iAssignMeterService.queryAssignMetersByBelongs(projectUID, communityUID, buildingUID, heatRoomUID);
		this.writeJsonStrToResponse(JSONUtil.serialize(meterList));
		return Action.SUCCESS;
	}
	
	/**
	 * 通过热用户ID查询所属的分配表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2013-9-4
	 */
	public String queryMetersByHeatRoomUID() throws Exception {
		if(this.heatRoomUID!=null && !"".equals(heatRoomUID)) {
			this.heatRoom = this.iHeatRoomService.queryEntityById(heatRoomUID);
			//所属项目、小区、楼栋信息
			this.projectUID = this.heatRoom.getProjectID();
			this.communityUID = this.heatRoom.getCommunityID();
			this.buildingUID = this.heatRoom.getBuildingID();
		}
		//分页信息
		this.pageCond = new PageCond();
		int start = (this.pageIndex - 1) * this.pageCond.getLimit();
		this.pageCond.setStart(start);
		//分页结果
		this.assignMeterPage = this.iAssignMeterService.queryMeterPageByRoomUID(seriousNum, projectUID, communityUID, buildingUID, heatRoomUID, pageCond);
		//计算结果集的页数
		this.pageSize = new BigDecimal(this.assignMeterPage.getPageCond().getTotal())
				.divide(new BigDecimal(this.assignMeterPage.getPageCond().getLimit()), RoundingMode.UP).intValue();
		if(pageSize == 0) pageSize = 1;
		//散热器id-sinkNo映射表
		this.pageSinkerMap = this.iAssignMeterService.queryReferHeatSinkerMap(assignMeterPage.getList());
		return "roomMeterPage";
	}
	
	/**
	 * 通过集中器信息查询所属分配表列表
	 * @return
	 * @throws Exception
	 * @author Xiaopeng
	 * @date 2014-3-17
	 */
	public String queryMetersByConcentratorUID() throws Exception {
		//根据集中器查询所属分配表列表
		this.assignMeterList = this.iAssignMeterService.queryMetersByConcentratorID(concentratorUID);
		//查询分配表对应的楼栋映射表
		this.referBuildingMap = this.iAssignMeterService.queryReferBuildingMap(assignMeterList);
		//查询分配表对应的热用户映射表
		this.referRoomMap = this.iAssignMeterService.queryReferHeatRoomMap(assignMeterList);
		return "conMeterList";
	}
	
	/**
	 * @return the iAssignMeterService
	 */
	@JSON(serialize=false)
	public IAssignMeterService getiAssignMeterService() {
		return iAssignMeterService;
	}
	
	/**
	 * @param iAssignMeterService the iAssignMeterService to set
	 */
	public void setiAssignMeterService(IAssignMeterService iAssignMeterService) {
		this.iAssignMeterService = iAssignMeterService;
	}

	/**
	 * @return the iProjectService
	 */
	@JSON(serialize=false)
	public IProjectService getiProjectService() {
		return iProjectService;
	}

	/**
	 * @param iProjectService the iProjectService to set
	 */
	public void setiProjectService(IProjectService iProjectService) {
		this.iProjectService = iProjectService;
	}

	/**
	 * @return the iCommunityService
	 */
	@JSON(serialize=false)
	public ICommunityService getiCommunityService() {
		return iCommunityService;
	}

	/**
	 * @param iCommunityService the iCommunityService to set
	 */
	public void setiCommunityService(ICommunityService iCommunityService) {
		this.iCommunityService = iCommunityService;
	}

	/**
	 * @return the iBuildingService
	 */
	@JSON(serialize=false)
	public IBuildingService getiBuildingService() {
		return iBuildingService;
	}

	/**
	 * @param iBuildingService the iBuildingService to set
	 */
	public void setiBuildingService(IBuildingService iBuildingService) {
		this.iBuildingService = iBuildingService;
	}

	/**
	 * @return the iHeatRoomService
	 */
	@JSON(serialize=false)
	public IHeatRoomService getiHeatRoomService() {
		return iHeatRoomService;
	}

	/**
	 * @param iHeatRoomService the iHeatRoomService to set
	 */
	public void setiHeatRoomService(IHeatRoomService iHeatRoomService) {
		this.iHeatRoomService = iHeatRoomService;
	}

	/**
	 * @return the iSinkBiz
	 */
	@JSON(serialize=false)
	public ISinkBiz getiSinkBiz() {
		return iSinkBiz;
	}

	/**
	 * @param iSinkBiz the iSinkBiz to set
	 */
	public void setiSinkBiz(ISinkBiz iSinkBiz) {
		this.iSinkBiz = iSinkBiz;
	}

	/**
	 * @return the iConcentratorService
	 */
	@JSON(serialize=false)
	public IConcentratorService getiConcentratorService() {
		return iConcentratorService;
	}

	/**
	 * @param iConcentratorService the iConcentratorService to set
	 */
	public void setiConcentratorService(IConcentratorService iConcentratorService) {
		this.iConcentratorService = iConcentratorService;
	}

	/**
	 * @return the seriousNum
	 */
	public String getSeriousNum() {
		return seriousNum;
	}

	/**
	 * @param seriousNum the seriousNum to set
	 */
	public void setSeriousNum(String seriousNum) {
		this.seriousNum = seriousNum;
	}

	/**
	 * @return the projectUID
	 */
	public String getProjectUID() {
		return projectUID;
	}

	/**
	 * @param projectUID the projectUID to set
	 */
	public void setProjectUID(String projectUID) {
		this.projectUID = projectUID;
	}

	/**
	 * @return the communityUID
	 */
	public String getCommunityUID() {
		return communityUID;
	}

	/**
	 * @param communityUID the communityUID to set
	 */
	public void setCommunityUID(String communityUID) {
		this.communityUID = communityUID;
	}

	/**
	 * @return the buildingUID
	 */
	public String getBuildingUID() {
		return buildingUID;
	}

	/**
	 * @param buildingUID the buildingUID to set
	 */
	public void setBuildingUID(String buildingUID) {
		this.buildingUID = buildingUID;
	}

	/**
	 * @return the heatRoomUID
	 */
	public String getHeatRoomUID() {
		return heatRoomUID;
	}

	/**
	 * @param heatRoomUID the heatRoomUID to set
	 */
	public void setHeatRoomUID(String heatRoomUID) {
		this.heatRoomUID = heatRoomUID;
	}

	/**
	 * @return the projectList
	 */
	public List<IProject> getProjectList() {
		return projectList;
	}

	/**
	 * @param projectList the projectList to set
	 */
	public void setProjectList(List<IProject> projectList) {
		this.projectList = projectList;
	}

	/**
	 * @return the communityList
	 */
	public List<ICommunity> getCommunityList() {
		return communityList;
	}

	/**
	 * @param communityList the communityList to set
	 */
	public void setCommunityList(List<ICommunity> communityList) {
		this.communityList = communityList;
	}

	/**
	 * @return the buildingList
	 */
	public List<IBuilding> getBuildingList() {
		return buildingList;
	}

	/**
	 * @param buildingList the buildingList to set
	 */
	public void setBuildingList(List<IBuilding> buildingList) {
		this.buildingList = buildingList;
	}

	/**
	 * @return the heatRoomList
	 */
	public List<IHeatRoom> getHeatRoomList() {
		return heatRoomList;
	}

	/**
	 * @param heatRoomList the heatRoomList to set
	 */
	public void setHeatRoomList(List<IHeatRoom> heatRoomList) {
		this.heatRoomList = heatRoomList;
	}

	/**
	 * @return the pageIndex
	 */
	public int getPageIndex() {
		return pageIndex;
	}

	/**
	 * @param pageIndex the pageIndex to set
	 */
	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	/**
	 * @return the pageCond
	 */
	public PageCond getPageCond() {
		return pageCond;
	}

	/**
	 * @param pageCond the pageCond to set
	 */
	public void setPageCond(PageCond pageCond) {
		this.pageCond = pageCond;
	}

	/**
	 * @return the pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * @param pageSize the pageSize to set
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * @return the assignMeterPage
	 */
	public ResultWithPageCond<IAssignMeter> getAssignMeterPage() {
		return assignMeterPage;
	}

	/**
	 * @param assignMeterPage the assignMeterPage to set
	 */
	public void setAssignMeterPage(ResultWithPageCond<IAssignMeter> assignMeterPage) {
		this.assignMeterPage = assignMeterPage;
	}

	/**
	 * @return the pageConcentratorMap
	 */
	public Map<String, String> getPageConcentratorMap() {
		return pageConcentratorMap;
	}

	/**
	 * @param pageConcentratorMap the pageConcentratorMap to set
	 */
	public void setPageConcentratorMap(Map<String, String> pageConcentratorMap) {
		this.pageConcentratorMap = pageConcentratorMap;
	}

	/**
	 * @return the pageSinkerMap
	 */
	public Map<String, String> getPageSinkerMap() {
		return pageSinkerMap;
	}

	/**
	 * @param pageSinkerMap the pageSinkerMap to set
	 */
	public void setPageSinkerMap(Map<String, String> pageSinkerMap) {
		this.pageSinkerMap = pageSinkerMap;
	}

	/**
	 * @return the assignMeterUID
	 */
	public String getAssignMeterUID() {
		return assignMeterUID;
	}

	/**
	 * @param assignMeterUID the assignMeterUID to set
	 */
	public void setAssignMeterUID(String assignMeterUID) {
		this.assignMeterUID = assignMeterUID;
	}

	/**
	 * @return the assignMeter
	 */
	public IAssignMeter getAssignMeter() {
		return assignMeter;
	}

	/**
	 * @param assignMeter the assignMeter to set
	 */
	public void setAssignMeter(IAssignMeter assignMeter) {
		this.assignMeter = assignMeter;
	}

	/**
	 * @return the heatSinkerList
	 */
	public List<PSinkInfo> getHeatSinkerList() {
		return heatSinkerList;
	}

	/**
	 * @param heatSinkerList the heatSinkerList to set
	 */
	public void setHeatSinkerList(List<PSinkInfo> heatSinkerList) {
		this.heatSinkerList = heatSinkerList;
	}

	/**
	 * @return the concentratorList
	 */
	public List<IConcentrator> getConcentratorList() {
		return concentratorList;
	}

	/**
	 * @param concentratorList the concentratorList to set
	 */
	public void setConcentratorList(List<IConcentrator> concentratorList) {
		this.concentratorList = concentratorList;
	}

	/**
	 * @return the project
	 */
	public IProject getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(IProject project) {
		this.project = project;
	}

	/**
	 * @return the community
	 */
	public ICommunity getCommunity() {
		return community;
	}

	/**
	 * @param community the community to set
	 */
	public void setCommunity(ICommunity community) {
		this.community = community;
	}

	/**
	 * @return the building
	 */
	public IBuilding getBuilding() {
		return building;
	}

	/**
	 * @param building the building to set
	 */
	public void setBuilding(IBuilding building) {
		this.building = building;
	}

	/**
	 * @return the heatRoom
	 */
	public IHeatRoom getHeatRoom() {
		return heatRoom;
	}

	/**
	 * @param heatRoom the heatRoom to set
	 */
	public void setHeatRoom(IHeatRoom heatRoom) {
		this.heatRoom = heatRoom;
	}

	/**
	 * @return the concentrator
	 */
	public IConcentrator getConcentrator() {
		return concentrator;
	}

	/**
	 * @param concentrator the concentrator to set
	 */
	public void setConcentrator(IConcentrator concentrator) {
		this.concentrator = concentrator;
	}

	/**
	 * @return the heatSinker
	 */
	public PSinkInfo getHeatSinker() {
		return heatSinker;
	}

	/**
	 * @param heatSinker the heatSinker to set
	 */
	public void setHeatSinker(PSinkInfo heatSinker) {
		this.heatSinker = heatSinker;
	}

	/**
	 * @return the concentratorUID
	 */
	public String getConcentratorUID() {
		return concentratorUID;
	}

	/**
	 * @param concentratorUID the concentratorUID to set
	 */
	public void setConcentratorUID(String concentratorUID) {
		this.concentratorUID = concentratorUID;
	}

	/**
	 * @return the assignMeterList
	 */
	public List<IAssignMeter> getAssignMeterList() {
		return assignMeterList;
	}

	/**
	 * @param assignMeterList the assignMeterList to set
	 */
	public void setAssignMeterList(List<IAssignMeter> assignMeterList) {
		this.assignMeterList = assignMeterList;
	}

	/**
	 * @return the referBuildingMap
	 */
	public Map<String, String> getReferBuildingMap() {
		return referBuildingMap;
	}

	/**
	 * @param referBuildingMap the referBuildingMap to set
	 */
	public void setReferBuildingMap(Map<String, String> referBuildingMap) {
		this.referBuildingMap = referBuildingMap;
	}

	/**
	 * @return the referRoomMap
	 */
	public Map<String, String> getReferRoomMap() {
		return referRoomMap;
	}

	/**
	 * @param referRoomMap the referRoomMap to set
	 */
	public void setReferRoomMap(Map<String, String> referRoomMap) {
		this.referRoomMap = referRoomMap;
	}
	
}
